package com.tomshushu.doc.freedoc.modular.projectuser.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tomshushu.doc.freedoc.core.exception.ServiceException;
import com.tomshushu.doc.freedoc.modular.projectuser.entity.ProjectUserEntity;
import com.tomshushu.doc.freedoc.modular.projectuser.mapper.ProjectUserMapper;
import com.tomshushu.doc.freedoc.modular.projectuser.model.param.AddProjectUserParam;
import com.tomshushu.doc.freedoc.modular.projectuser.model.param.GetUserByProjectParam;
import com.tomshushu.doc.freedoc.modular.projectuser.model.param.UpdatePermissionPram;
import com.tomshushu.doc.freedoc.modular.projectuser.model.result.GetUserByProjectResult;
import com.tomshushu.doc.freedoc.modular.projectuser.service.ProjectUserService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: zhouhong
 * @date: 2025-06-11 12:47
 * @version: 1.0
 */
@Service
public class ProjectUserServiceImpl  extends ServiceImpl<ProjectUserMapper, ProjectUserEntity> implements ProjectUserService {

    @Resource
    private ProjectUserMapper projectUserMapper;

    @Override
    public void addProjectUser(AddProjectUserParam param) {
        if (param.getProjectUserList().isEmpty()) {
            return;
        }
        param.getProjectUserList().forEach(item -> {
            // 校验是否已经关联
            long count = this.count(new QueryWrapper<ProjectUserEntity>()
                    .eq("user_id", item.getUserId())
                    .eq("project_id", item.getProjectId()));
            if (count > 0) {
                throw new ServiceException(7006, "用户已存在当前项目");
            }
            ProjectUserEntity projectUserEntity = new ProjectUserEntity();
            BeanUtils.copyProperties(item, projectUserEntity);
            if (ObjectUtil.isNull(item.getType())) {
                projectUserEntity.setType("PARTICIPANT");
            }
            save(projectUserEntity);
        });
    }

    @Override
    public void deleteProjectUser(AddProjectUserParam param) {
        if (param.getProjectUserList().isEmpty()) {
            return;
        }
        param.getProjectUserList().forEach(item -> {
            this.remove(new LambdaQueryWrapper<ProjectUserEntity>()
                    .eq(ProjectUserEntity::getUserId, item.getUserId())
                    .eq(ProjectUserEntity::getProjectId, item.getProjectId())
                    .ne(ProjectUserEntity::getType, "OWNER"));
        });
    }

    @Override
    public void updatePermission(UpdatePermissionPram pram) {
        ProjectUserEntity projectUser = this.getOne(new QueryWrapper<ProjectUserEntity>()
                .eq("user_id", pram.getUserId())
                .eq("project_id", pram.getProjectId()));
        if (ObjectUtil.isNull(projectUser)) {
            throw new ServiceException(7007, "用户不存在");
        }
        projectUser.setPermission(pram.getPermission());
        updateById(projectUser);
    }

    @Override
    public List<GetUserByProjectResult> getUserByProjectId(GetUserByProjectParam pram) {
        if (pram.getProjectId() == null) {
            throw new ServiceException("参数错误");
        }
        return projectUserMapper.getUserByProjectId(pram.getProjectId());
    }
}